/*
 SAMPLE FORM
*/
var qs = require('querystring');

require('./common');


exports.load = function(req, res){
	
	var strSQL = "SELECT FormName, Name, Description, Content, Buttons FROM tblworkspace WHERE ID = " + conn.escape(req.params.ID) + " ";
	var requestID = req.params.requestID;
	var content, name, description, formName, buttons;
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		
		for (var index in results) {
			formName = results[index].FormName;
			content = results[index].Content;
			name = results[index].Name;
			description = results[index].Description;
			buttons = results[index].Buttons;
		}
	
		updateItemInSQL(name, "UnRead", "0" , "ID", requestID, "");
		
		res.render("form",{
			title: formName,
			name: name,
			content: content,
			buttons: buttons,
			description: description,
			requestID: requestID
		});
	});

}

exports.save = function(req, res){

	var tblName = req.body.tblName;
	var mode = req.body.requestID;
	var strSQL = "SELECT COLUMN_NAME FROM information_schema.columns where table_name=" + conn.escape(tblName);
	var fieldName = new Array();
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		
		for (var index in results) {
			fieldName[fieldName.length] = results[index].COLUMN_NAME;
		}
		
		console.log(fieldName.join());
		if(mode=="0"){
			insertRecord(tblName,fieldName, req);
		}else{
			updateRecord(tblName,fieldName, req);
		}
		
		res.redirect('/');
	});
	
}

